﻿using FM.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FM.Service
{
    public class CategoryService : ICategoryService
    {
        private readonly IRepository<Category> categoryRepository;
        private readonly IUnitOfWork unitOfWork;
        public CategoryService(IRepository<Category> categoryRepository,IUnitOfWork unitOfWork)
        {
            this.categoryRepository = categoryRepository;
            this.unitOfWork = unitOfWork;
        }
        #region ICategoryService Members

        public IEnumerable<Category> GetCategories()
        {
            var categories = categoryRepository.GetAll();
            return categories;
        }

        public Category GetCategory(int id)
        {
            var category = categoryRepository.GetById(id);
            return category;
        }

        public void CreateCategory(Category category)
        {
            categoryRepository.Add(category);
            Save();
        }

        public void UpdateCategory(Category category)
        {
            categoryRepository.Update(category);
            Save();
        }

        public void DeleteCategory(int id)
        {
            var category = categoryRepository.GetById(id);
            categoryRepository.Delete(category);
            Save();
        }

        private void Save()
        {
            unitOfWork.Commit();
        }

        #endregion
    }
}
